package com.example.myapplication;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;


/**
 * 用于将Button进行事件绑定，每次点击时，会发布Intent（str）广播。
 */
public class InitItemsEvent {
    private IntentFilter intentFilter;
    private BroadcastReceiver myBro;
    private AppCompatActivity app;


    public InitItemsEvent(AppCompatActivity app, BroadcastReceiver myBro) {
        this.myBro = myBro;
        intentFilter = new IntentFilter();
        this.app = app;
    }

    public boolean addAction(String str) {
        intentFilter.addAction(str);
        return true;
    }

    public boolean registerReceiver(Activity app) {
        app.registerReceiver(myBro, intentFilter);
        return true;
    }

    public boolean registerReceiver() {
        app.registerReceiver(myBro, intentFilter);
        return true;
    }

    public boolean registerReceiver(AppCompatActivity app) {
        app.registerReceiver(myBro, intentFilter);
        return true;
    }

    public <E extends View> boolean ItemToEvent(E e, final String str) {
        e.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Log.e("e.setOnClickListener---" + str, "public <E extends View> boolean ItemToEvent(E e,final String str){");
                app.sendBroadcast(new Intent(str));
            }
        });
        return true;
    }

    public <E extends View, T extends View.OnTouchListener> boolean ItemToEvent(E e, T t) {
        e.setOnTouchListener(t);
        return true;
    }

    public <E extends View, T extends View.OnClickListener> boolean ItemToEvent(E e, T t) {
        e.setOnClickListener(t);
        return true;
    }

    public boolean unregisterReceiver() {
        app.unregisterReceiver(myBro);
        return true;
    }
}
